package com.LeeCode;

/**
 * 二叉树剪枝
 */

public class Code814 {
    public static void main(String[] args) {

    }

    public TreeNode pruneTree(TreeNode root) {
        if (root == null) return null;
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.left == null && root.right == null && root.val == 0) return null;

        return root;
    }

    public TreeNode pruneTree1(TreeNode root) {
        if (dfs(root)) return null;

        return root;
    }

    private boolean dfs(TreeNode root) {
        if (root == null) return true;

        boolean left = dfs(root.left);
        if (left) root.left = null;

        boolean right = dfs(root.right);
        if (right) root.right = null;
        return left && right && root.val == 0;
    }


}
